package com.huaru.formboot.form.service.impl;

import com.huaru.formboot.form.dao.ComponentTemplateMapper;
import com.huaru.formboot.form.entity.Dto.ComponentTemplate;
import com.huaru.formboot.form.service.ComponentTemplateService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 组件模板表 服务实现类
 * </p>
 *
 * @author wz
 * @since 2021-04-15
 */
@Service
public class ComponentTemplateServiceImpl implements ComponentTemplateService {
   @Autowired
   private ComponentTemplateMapper componentTemplateMapper;
   //获取所有组件
    @Override
    public List<ComponentTemplate> getAllComponentTemplate() {

        return componentTemplateMapper.getAllComponentTemplate();
    }

    /**
     * 新增组件
     * @param
     * @return
     */
    @Override
    public Integer insertComponentTemplate(ComponentTemplate componentTemplate) {
        ComponentTemplate componentTemp = new ComponentTemplate(
                UUID.randomUUID().toString(),
                componentTemplate.getComponentName(),
                componentTemplate.getComponentType(),
                componentTemplate.getComponentDesc(),
                componentTemplate.getComponentSort(),
                componentTemplate.getRowsLimit(),
                componentTemplate.getElement(),
                LocalDateTime.now(),
                LocalDateTime.now()
        );
        return componentTemplateMapper.insertComponentTemplate(componentTemp);
    }

    /**
     * 根据组件模糊查询
     * @param componentName
     * @return
     */
    @Override
    public List<ComponentTemplate> getComponentTemplateByName(String componentName) {


        return   componentTemplateMapper.getComponentTemplateByName(componentName);
    }

    /**
     * 删除组件
     * @param componentId
     * @return
     */
    @Override
    public Integer deleteByComponentId(String componentId) {

        return componentTemplateMapper.deleteByComponentId(componentId);
    }
  /**
   *更新组件
   */
    @Override
    public Integer updateComponentTemplateById(ComponentTemplate componentTemplate) {
        ComponentTemplate componentTemp = new ComponentTemplate(
                componentTemplate.getComponentId(),
                componentTemplate.getComponentName(),
                componentTemplate.getComponentType(),
                componentTemplate.getComponentDesc(),
                componentTemplate.getComponentSort(),
                componentTemplate.getRowsLimit(),
                componentTemplate.getElement(),
                null,
                LocalDateTime.now()
        );
        return componentTemplateMapper.updateComponentTemplateById(componentTemp);
    }

    @Override
    public Integer updateElementStyle(ComponentTemplate componentTemplate) {
        ComponentTemplate componentTemp= new ComponentTemplate();
        componentTemp.setComponentId(componentTemplate.getComponentId());
        componentTemp.setElement(componentTemplate.getElement());
        componentTemp.setUpdateTime(LocalDateTime.now());
        return componentTemplateMapper.updateComponentTemplateById(componentTemp);
    }
}
